package com.dianrui.qiyouriding.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.adapter.MyAgreementFragmnetAdapter;
import com.dianrui.qiyouriding.event.RfreshAgreement;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 查看合同
 * @author lzy
 */
public class AgreeMentActivity extends BaseActivity {

    public static int fragemntPosition = 0;

    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.pager)
    ViewPager pager;

    @BindView(R.id.tablayout)
    TabLayout tablayout;


    @Override
    public int getContentViewId() {
        return R.layout.activity_agreement;
    }

    @Override
    public void initView() {
        customInit(true, R.color.main_color);
        title.setText(getString(R.string.agreement_my));
        FragmentManager supportFragmentManager = getSupportFragmentManager();

        MyAgreementFragmnetAdapter adapter = new MyAgreementFragmnetAdapter(supportFragmentManager, AgreeMentActivity.this);

        pager.setAdapter(adapter);


        tablayout.setupWithViewPager(pager);

        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                fragemntPosition = tab.getPosition();
                EventBus.getDefault().post(new RfreshAgreement(fragemntPosition));
                pager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        pager.setCurrentItem(fragemntPosition);

        EventBus.getDefault().post(new RfreshAgreement(fragemntPosition));

        pager.setOffscreenPageLimit(2);


    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                fragemntPosition = 0;
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            fragemntPosition = 0;
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
